      1  import java.awt.BorderLayout;

      2  import java.awt.Font;

      3  import java.awt.GridLayout;

      4  import java.awt.event.ActionEvent;

      5  import java.awt.event.ActionListener;

      6  import javax.swing.ButtonGroup;

      7  import javax.swing.JButton;

      8  import javax.swing.JCheckBox;

      9  import javax.swing.JComboBox;

     10  import javax.swing.JFrame;

     11  import javax.swing.JLabel;

     12  import javax.swing.JPanel;

     13  import javax.swing.JRadioButton;

     14  import javax.swing.border.EtchedBorder;

     15  import javax.swing.border.TitledBorder;

     16

     17  /**

     18     This frame contains a text sample and a control panel

     19     to change the font of the text.

     20  */

     21  public class FontFrame extends JFrame

     22  {

     23     private static final int FRAME_WIDTH = 300;

     24     private static final int FRAME_HEIGHT = 400;

     25

     26     private JLabel label;

     27     private JCheckBox italicCheckBox;

     28     private JCheckBox boldCheckBox;

     29     private JRadioButton smallButton;

     30     private JRadioButton mediumButton;

     31     private JRadioButton largeButton;

     32     private JComboBox facenameCombo;

     33     private ActionListener listener;

     34

     35     /**

     36        Constructs the frame.

     37     */

     38     public FontFrame()

     39     {

     40        // Construct text sample

     41        label = new JLabel("Big Java");

     42        add(label, BorderLayout.CENTER);

     43

     44        // This listener is shared among all components

     45        listener = new ChoiceListener();

     46

     47        createControlPanel();

     48        setLabelFont();

     49        setSize(FRAME_WIDTH, FRAME_HEIGHT);

     50     }

     51

     52     class ChoiceListener implements ActionListener

     53     {

     54        public void actionPerformed(ActionEvent event)

     55        {

     56           setLabelFont();

     57        }

     58     }

     59

     60     /**

     61        Creates the control panel to change the font.

     62     */

     63     public void createControlPanel()

     64     {

     65        JPanel facenamePanel = createComboBox();

     66        JPanel sizeGroupPanel = createCheckBoxes();

     67        JPanel styleGroupPanel = createRadioButtons();

     68

     69        // Line up component panels

     70

     71        JPanel controlPanel = new JPanel();

     72        controlPanel.setLayout(new GridLayout(3, 1));

     73        controlPanel.add(facenamePanel);

     74        controlPanel.add(sizeGroupPanel);

     75        controlPanel.add(styleGroupPanel);

     76

     77        // Add panels to content pane

     78

     79        add(controlPanel, BorderLayout.SOUTH);

     80     }

     81

     82     /**

     83        Creates the combo box with the font style choices.

     84        @return the panel containing the combo box

     85     */

     86     public JPanel createComboBox()

     87     {

     88        facenameCombo = new JComboBox();

     89        facenameCombo.addItem("Serif");

     90        facenameCombo.addItem("SansSerif");

     91        facenameCombo.addItem("Monospaced");

     92        facenameCombo.setEditable(true);

     93        facenameCombo.addActionListener(listener);

     94

     95        JPanel panel = new JPanel();

     96        panel.add(facenameCombo);

     97        return panel;

     98     }

     99

    100     /**

    101        Creates the check boxes for selecting bold and italic styles.

    102        @return the panel containing the check boxes

    103     */

    104     public JPanel createCheckBoxes()

    105     {

    106        italicCheckBox = new JCheckBox("Italic");

    107        italicCheckBox.addActionListener(listener);

    108

    109        boldCheckBox = new JCheckBox("Bold");

    110        boldCheckBox.addActionListener(listener);

    111

    112        JPanel panel = new JPanel();

    113        panel.add(italicCheckBox);

    114        panel.add(boldCheckBox);

    115        panel.setBorder(new TitledBorder(new EtchedBorder(), "Style"));

    116

    117        return panel;

    118     }

    119

    120     /**

    121        Creates the radio buttons to select the font size.

    122        @return the panel containing the radio buttons

    123     */

    124     public JPanel createRadioButtons()

    125     {

    126        smallButton = new JRadioButton("Small");

    127        smallButton.addActionListener(listener);

    128

    129        mediumButton = new JRadioButton("Medium");

    130        mediumButton.addActionListener(listener);

    131

    132        largeButton = new JRadioButton("Large");

    133        largeButton.addActionListener(listener);

    134        largeButton.setSelected(true);

    135

    136        // Add radio buttons to button group

    137

    138        ButtonGroup group = new ButtonGroup();

    139        group.add(smallButton);

    140        group.add(mediumButton);

    141        group.add(largeButton);

    142

    143        JPanel panel = new JPanel();

    144        panel.add(smallButton);

    145        panel.add(mediumButton);

    146        panel.add(largeButton);

    147        panel.setBorder(new TitledBorder(new EtchedBorder(), "Size"));

    148

    149        return panel;

    150     }

    151

    152     /**

    153        Gets user choice for font name, style, and size

    154        and sets the font of the text sample.

    155     */

    156     public void setLabelFont()

    157     {

    158        // Get font name

    159        String facename = (String) facenameCombo.getSelectedItem();

    160

    161        // Get font style

    162

    163        int style = 0;

    164        if (italicCheckBox.isSelected())

    165        {

    166           style = style + Font.ITALIC;

    167        }

    168        if (boldCheckBox.isSelected())

    169        {

    170           style = style + Font.BOLD;

    171        }

    172

    173        // Get font size

    174

    175        int size = 0;

    176

    177        final int SMALL_SIZE = 24;

    178        final int MEDIUM_SIZE = 36;

    179        final int LARGE_SIZE = 48;

    180

    181        if (smallButton.isSelected()) { size = SMALL_SIZE; }

    182        else if (mediumButton.isSelected()) { size = MEDIUM_SIZE; }

    183        else if (largeButton.isSelected()) { size = LARGE_SIZE; }

    184

    185        // Set font of text field

    186

    187        label.setFont(new Font(facename, style, size));

    188        label.repaint();

    189     }

    190   }